<template>
    <view class="orders">
        <u-tabs
            :list="tabList"
            lineColor="#FC721A"
            :activeStyle="{
                color: '#FC721A',
                fontWeight: 'bold',
                transform: 'scale(1.05)',
            }"
            :inactiveStyle="{
                color: '#1C120D',
                transform: 'scale(1)',
            }"
            @click="handleTab"
        ></u-tabs>
        <view class="order_date">
            <view class="date_item" style="margin-right: 32rpx">
                <p>Start Date</p>
                <view class="item_choose" @click.stop="startShow = true">
                    <text>{{
                        startDate ? formatDate(startDate) : "Select date"
                    }}</text>
                    <image src="/static/business/time.png" mode=""></image>
                </view>
            </view>
            <view class="date_item">
                <p>End Date</p>
                <view class="item_choose" @click.stop="endShow = true">
                    <text>{{
                        endDate ? formatDate(endDate) : "Select date"
                    }}</text>
                    <image src="/static/business/time.png" mode=""></image>
                </view>
            </view>
        </view>
        <ul>
            <li
                v-for="(item, index) in list"
                :key="index"
                @click.stop="handleDetails(item)"
            >
                <dl>
                    <dt>
                        <image :src="item.headimgurl" mode=""></image>
                    </dt>
                    <dd>
                        <view class="orders_title">
                            <p>{{ item.username }}</p>
                            <!-- <text>To be used</text> -->
                            <text v-if="item.state == 0">Pending Payment</text>
                            <text v-else-if="item.state == 1">To be used</text>
                            <text v-else-if="item.state == 2"
                                >To be evaluated</text
                            >
                            <text
                                style="color: #000"
                                v-else-if="item.state == 3"
                                >Completed</text
                            >
                            <text v-else-if="item.state == '-1'"
                                >Pending refund</text
                            >
                            <text
                                style="color: #000"
                                v-else-if="item.state == '-2'"
                                >Refunded</text
                            >
                            <text
                                style="color: gray"
                                v-else-if="item.state == '-3'"
                                >Canceled</text
                            >
                        </view>
                        <view class="orders_item">
                            <p>
                                Payment amount:
                                <text>{{ item.pay_money || "0.00" }}Egp</text>
                            </p>
                            <text>x1</text>
                        </view>
                        <p v-if="item.origin_money">
                            Original amount: {{ item.origin_money }}Egp
                        </p>
                        <p v-if="item.discount_money">
                            Discount amount: {{ item.discount_money }}Egp
                        </p>
                        <p v-if="item.coupon_money">
                            Coupon deduction amount: {{ item.coupon_money }}Egp
                        </p>
                        <p class="orders_time">{{ item.ctime }}</p>
                    </dd>
                </dl>
            </li>
            <u-loadmore v-if="hasMore" :status="loadStatus" />
        </ul>
        <u-datetime-picker
            :show="startShow"
            v-model="startDate"
            mode="date"
            @confirm="handleStart"
        ></u-datetime-picker>
        <u-datetime-picker
            :show="endShow"
            v-model="endDate"
            mode="date"
            @confirm="handleEnd"
        ></u-datetime-picker>
    </view>
</template>

<script>
import { getBusinessOrder } from "@/lib/business.js";
import { showFailure } from "@/lib/helper";
import dayjs from "dayjs";
export default {
    data() {
        return {
            startShow: false,
            endShow: false,
            startDate: "",
            endDate: "",
            list: [],
            loadStatus: "loadmore",
            pageNo: 1,
            pageSize: 10,
            hasMore: false,
            state: "",
            tabList: [
                {
                    name: "All Orders",
                    state: "",
                },
                {
                    name: "To be used",
                    state: "1",
                },
                {
                    name: "Completed",
                    state: "2",
                },
                {
                    name: "Awaiting evaluation",
                    state: "-1",
                },
                {
                    name: "Rated",
                    state: "3",
                },
            ],
        };
    },
    onShow() {
        this.loadData(true);
    },
    onReachBottom() {
        if (this.hasMore) {
            this.pageNo = this.pageNo + 1;
            this.loadData();
        }
    },
    methods: {
        formatDate(timestamp) {
            return dayjs(timestamp).format("YYYY-MM-DD");
        },
        loadData(isReset) {
            let _this = this;
            this.loadStatus = "loading";
            let obj = {
                pageNo: _this.pageNo,
                pageSize: _this.pageSize,
            };
            if (_this.state) {
                obj["state"] = _this.state;
            }
            if (_this.startDate) {
                obj["startDate"] = _this.startDate;
            }
            if (_this.endDate) {
                obj["endDate"] = _this.endDate;
            }
            getBusinessOrder(obj)
                .then((res) => {
                    uni.hideLoading();
                    this.loadStatus = "no-more";
                    this.hasMore = res.data.hasMore;
                    if (isReset) {
                        this.list = res.data.records;
                    } else {
                        this.list = this.list.concat(res.data.records);
                    }
                })
                .catch((res) => {
                    this.loadStatus = "no-more";
                    uni.hideLoading();
                    showFailure();
                });
        },
        handleTab(e) {
            console.log(e);
            this.state = e.state;
            this.loadData(true);
        },
        handleStart(e) {
            this.startDate = dayjs(e.value).format("YYYY-MM-DD");
            console.log("startDate", this.startDate);
            this.startShow = false;
        },
        handleEnd(e) {
            this.endDate = dayjs(e.value).format("YYYY-MM-DD");
            console.log(this.endDate);
            this.endShow = false;
        },
        handleDetails(item) {
            uni.navigateTo({
                url: "./order_details?orderid=" + item.orderid,
            });
        },
    },
};
</script>

<style lang="scss" scoped>
.order_date {
    display: flex;
    padding: 32rpx 28rpx;

    .date_item {
        p {
            font-weight: 500;
            font-size: 28rpx;
            color: #666666;
        }

        .item_choose {
            width: 331rpx;
            height: 100rpx;
            background: #f3f4f6;
            border-radius: 16rpx;
            border: 2rpx solid #ccd1d7;
            margin-top: 16rpx;
            display: flex;
            justify-content: space-between;
            align-items: center;

            image {
                width: 40rpx;
                height: 40rpx;
                margin-right: 20rpx;
            }

            text {
                font-weight: 500;
                font-size: 28rpx;
                color: #b5b5b5;
                margin-left: 26rpx;
            }
        }
    }
}

ul {
    padding: 0 32rpx;

    li {
        padding: 24rpx 0;

        dl {
            display: flex;
            border-bottom: 1rpx solid #f2f2f2;

            dt {
                width: 108rpx;
                height: 108rpx;
                background-color: #f2f2f2;
                border-radius: 12rpx;
                margin-right: 20rpx;
                margin-top: 10rpx;

                image {
                    width: 100%;
                    height: 100%;
                }
            }

            dd {
                flex: 1;

                .orders_title {
                    display: flex;
                    justify-content: space-between;
                    align-items: center;

                    p {
                        font-size: 32rpx;
                        font-weight: bold;
                        color: #1c120d;
                    }

                    text {
                        font-size: 28rpx;
                        font-weight: 400;
                        color: #fc721a;
                    }
                }

                .orders_item {
                    display: flex;
                    justify-content: space-between;
                    align-items: center;
                    margin-top: 12rpx;

                    p {
                        font-size: 24rpx;
                        font-weight: 400;
                        color: #444444;

                        text {
                            font-size: 28rpx;
                            font-weight: 600;
                        }
                    }

                    > text {
                        font-size: 28rpx;
                        font-weight: 400;
                        color: #9e6b47;
                    }
                }

                p {
                    font-size: 24rpx;
                    font-weight: 400;
                    color: #444444;
                    margin-bottom: 10rpx;
                }

                .orders_time {
                    font-size: 24rpx;
                    font-weight: 400;
                    color: #888888;
                    margin-top: 10rpx;
                }
            }
        }
    }
}
</style>
